﻿using System;
using System.Xml;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using Ro.AssistFunction.BackNodeInfo;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.RoWebAction;
using Ro.Common.RoInterface.UserType;

namespace Ro.DriverEvent.WebAction
{
    /// <summary>
    /// Alert操作
    /// </summary>
    public class WebAlert : IWebAlertFunc
    {
        /// <summary>
        /// accept处理
        /// </summary>
        /// <param name="sigNode">对应节点</param>
        /// <returns>返回操作是否完成</returns>
        public WebActionResult Accept(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            try
            {
                WebDriverWait webDriverWait = new WebDriverWait(RoTestObject.WebTestObject, timeout);
                webDriverWait.Until(ExpectedConditions.AlertIsPresent()).Accept();
                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "Alert操作Accept遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("Accept", e);
            }
            return back;
        }


        /// <summary>
        /// dismiss处理
        /// </summary>
        /// <param name="sigNode">对应节点</param>
        /// <returns>返回操作</returns>
        public WebActionResult Dismiss(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;
            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);

            try
            {
                WebDriverWait webDriverWait = new WebDriverWait(RoTestObject.WebTestObject, timeout);
                webDriverWait.Until(ExpectedConditions.AlertIsPresent()).Dismiss();
                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "Alert操作Dismiss遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("Dismiss", e);
            }
            return back;
        }


        /// <summary>
        /// sendkeys处理
        /// </summary>
        /// <param name="sigNode">对应节点</param>
        /// <returns>返回操作</returns>
        public WebActionResult SendKeys(XmlNode sigNode)
        {
            //返回值
            WebActionResult back;

            ExtractNeedInfo extractNeedInfo = new ExtractNeedInfo();
            //提取超时
            TimeSpan timeout = extractNeedInfo.ExtractTimeout(sigNode);
            try
            {
                //输入
                WebDriverWait webDriverWait = new WebDriverWait(RoTestObject.WebTestObject, timeout);
                webDriverWait.Until(ExpectedConditions.AlertIsPresent()).SendKeys(sigNode.FirstChild.InnerText);
                back = BackActionResult.BackWebResult(true, "未使用", "没有附加信息"); //结果
            }
            catch (Exception e)
            {
                back = BackActionResult.BackWebResult(false, "未使用", "Alert操作SendKeys遭遇System异常"); //结果
                ExceptionOutPut.SystemExceptionOut("SendKeys", e);
            }
            return back;
        }
    }
}